package com.sam.im.heiqun.utils;

import java.io.IOException;
import java.io.InputStream;
import java.security.Key;
import java.security.PublicKey;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;



import com.google.gson.Gson;

/**
 * 接口参数加密RSA
 * @author Frelacdp
 *
 */
public class RequestRSAUtils {

	private static final String rsaPublicKey ="MIICojANBgkqhkiG9w0BAQEFAAOCAo8AMIICigKCAoEAxEQ4cqjoVoMJtIjl1T1VZ5VQBee+V4zW7k8AQrOBKxpvwWJEcQEEw2UdqwT/E/mMfTOoJpMS+n2doqVYuNQgfe/Tttha/INOEIUdR26NkbfWD36YLpIvsJBhpeDJHmt5hIZuEU0sERJPgu4A2Ie/g3VI4j5NvaahO4mpkx2GFApSFt17k9RzPlVaN29l3CJfYtf16Fl12ovTrsxCg8/DSLNxPKEPir3bTsHBzcz4lnqDuIUIVlGPf9t3IYUwIQbSbTuQU9LAcReNOt45RK43Cg52sZ0NuWN7/GuSBQFN/RG+9kkyeNhKCwfPGETrc9SZ/uEx4kakCyZgsv9Zfl14S/SWRorPFYmnrMRXw5ngdkCWOMwmqyCmg5R0SjMo5PdJ6KoBp2q79YSI3TrDoWw1Do75se+ENYAiorBy7MfIEOLOggs56BcJd8BFRDHdjAB1E001ci36wxx11Qb9uzlrqRSM31LygNT/lnHFrvBWQ+91UZuC2oeCxEfj1uh4zWy4VxqcBpFeJ3y0Lzs6gWadtu7gvYZPUXOMOrBJmGxW+e8Ha4fPYYSYuvJhLkXrJ0GnHHyh5Yc4lSwfse8FKdzDJ/ZUFlIz8wc5SYeiplW6uWmyBUEdTc4HfhAQrqB8FhqXOJYWorequPcjeU1+wRb9Hq0JW66jcGskM8/QuWyYqDVB1tfQnhUeRoJ6iii90IuyIiwkbcbguJf7Q8cXJg388ik7gx5BRNUVBjKjzo9xxv0pEUlZRgtWMwJNJmmLxpgaklq50/XmRXEJGV6DQ/cTlxSyMUFUJJp5i9c8CDf1wL5NYS/8vIJqVXNFyw7HYN8gFI69DbkWugc6smh1RCNgtQIDAQAB";
	private static String rsaPrivateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIiD0+Z92WT9jqQcO4jk0h8JIVujRUNvALqgnsd/nmzPH1cdNRWty/6lcjjO143qt6Ju3l4q7DS84gEQw+oofHTr92lGjFeIcmSwtq9NUQ8v6OQdJjFZSmCdfLcxoBYdrovV9ilOcAElUrUhHkCmp+VLxzG6+d9aFTIhDNF55BpxAgMBAAECgYA4Pl/6UZVUl2bCVGNu5h9v6/oTYDQ0MTKSsYRZW0nxOHCIGp95XA50zsM9hnKmpLKduKHjGDoTDFLs73RYL+/+PbjnQHv3nuMUACKAcCjKb1UFpxD6LCJ3DQueEKtzKHw0yXEd53B0/1MqI4feMpm46ocoOLcsk1CnRy8AwR3zxQJBAMbBPnt75QnwoNAI2fOysfhyUesD9ljLwSMvgKSyzszbRSHcA2szLjvYSBed5E3fKliARFpAMPkoPMF7jqDH+P8CQQCv1XW6eF5xZbiVZ1EZ2xg4p0kHdAMveiCMi+RMiR8dyPBttxOOK2JJqap/JxbHgWiHSUpZ23M/6P/N2WnB6/yPAkEAw8q58osqJZCV2lnd2uO9zjFP4WQV2YNez3qMD7LVZZjbkW9bLs9MTEfTPPCdZtjgri4tJ3r0w32P3EoLRve3oQJAVE02ZZGiTiHj7pWbV5cotLjXlaA0isW150yGx38SkRgu1Ztp38bDF+glm1LlY/+SWxJzeENzrReL1WjAgL4g1wJBAMIWqOjVI8XzzdX3IsVTJoVyj84YyFuGlM1J7Qw30IQMAMiH7zHclAw7HA3Kc/JKwlhwn4pI8NTb7oIcU4z7KMo=";
	/*public static String getPublicKey(Map<String, Object> keyMap)throws Exception{
		Key key = (Key) keyMap.get(PUBLIC_KEY);
		return encryptBASE64(key.getEncoded());
	}*/


	@SuppressWarnings({ "unused", "rawtypes" })
	private static void printAllProperty(Properties props) {  
		Enumeration en = props.propertyNames();  
	    while (en.hasMoreElements()) {  
            String key = (String) en.nextElement();  
            String value = props.getProperty(key);  
	    }  
	}


	/**
	 * Rea解密
	 */
	@SuppressWarnings("unchecked")
	public static String getRsaContent(String content){
		byte[] byte1 = null;
		byte[] byte2 = null;
		try {
			byte1 = RSAUtils.decryptBASE64(content);
			byte2 = RSAUtils.decryptByPrivateKey(byte1, rsaPrivateKey);
			content = new String(byte2);
			/*Gson gson = new Gson();
            Map<String, Object> maps = new HashMap<String, Object>();
            maps = gson.fromJson(content, maps.getClass());*/
            return content;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}



	/**
	 * Rea加密
	 */
	public static String setRsaContent(Map<String, Object> maps){
		byte[] result = null;
		String content = maps+"";
		try {
			result = RSAUtils.encryptByPublicKey(content.getBytes(), rsaPublicKey);
            content = RSAUtils.encryptBASE64(result);
            return content;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 验证接口请求时长
	 * @param timestamp
	 * @return
	 */
	public static boolean validateTime(long timestamp) {
		long time = System.currentTimeMillis() - 10*1000;
		if(time > timestamp) {
			return false;
		}else {
			return true;
		}
	}
	
	
}
